﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class PrototypeSeachProductView : BaseForm, ISeachProduct
    {
        private SearchProductPresenter pPresenter;

        #region CONSTRUCTOR

        public PrototypeSeachProductView()
        {
            InitializeComponent();
            pPresenter = new SearchProductPresenter(this);
        }

        #endregion

        #region EVENTS

        private void StoreView_Load(object sender, EventArgs e)
        {
            string ProductToSearch = string.Empty;
            if (Parameter != null)
            {
                ProductToSearch = (string)Parameter;
            }

            if (!string.IsNullOrEmpty(ProductToSearch))
            {
                txtSearch.Text = ProductToSearch;
                if (OnSearch != null)
                    OnSearch(this, new GenericEventArgs<string>(txtSearch.Text));
            }

        }

        private void tsAddItemToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (OnSave != null)
                    OnSave(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                if (OnSearch != null)
                    OnSearch(this, new GenericEventArgs<string>(txtSearch.Text));
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void grvData_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            try
            {
                if (OnSave != null)
                    OnSave(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        #endregion

        #region ISeachProduct Members

        public List<Product> ColProduct
        {
            set 
            {
                if (value == null)
                {
                    Context.ShowInformationMessageBox("No hubo resultados en la busqueda.", "AVISO");
                }
                grvData.DataSource = value;
            }
        }

        public Product Get()
        {
            throw new NotImplementedException();
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
            {
                Result = (Product)grvData.ActiveRow.ListObject;
            }
            else
            {
                Result = null;
            }            
            Close();
        }

        public event Innova.FoundationFramework.DataTypes.GenericEventHandler<string> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        public event GenericEventHandler<string> OnSearch;

        #endregion
        
    }
}
